from common.Base import Base, open_op_browser, open_browser
from time import sleep

login_url = "http://localhost:8080/ecshop/user.php"
path = r"C:\Users\GrapeLier\AppData\Roaming\Mozilla\Firefox\Profiles\iybiyjzp.default-release"


class Userinfo_page(Base):
    # 修改用户中心-个人资料的相关定位
    userinfo_loc = ("link text", "用户信息")
    birthday_year_loc = ("name", "birthdayYear")
    birthday_month_loc = ("name", "birthdayMonth")
    birthday_day_loc = ("name", "birthdayDay")
    women_loc = ("css selector", "input[value='2']")
    man_loc = ("css selector", "input[value='1']")
    secrecy_loc = ("css selector", "input[value='0']")
    email_loc = ("name", "email")
    extend_field5_loc = ("name", "extend_field5")
    qq_loc = ("name", "extend_field2")
    office_tel_loc = ("name", "extend_field3")
    home_tel_loc = ("name", "extend_field4")
    question_loc = ("name", "sel_question")
    answer_loc = ("name", "passwd_answer")
    userinfo_enter_loc = ("css selector", "form[name='formEdit'] input[value='确认修改']")

    # 修改用户中心-用户密码相关定位
    old_password_loc = ("name", "old_password")
    new_password_loc = ("name", "new_password")
    chick_password_loc = ("name", "comfirm_password")
    password_enter_loc = ("css selector", "form[name='formPassword'] input[value='确认修改']")

    def click_userinfo(self):
        """
        定位用户信息
        :return:
        """
        self.click(self.userinfo_loc)

    def birthday_year(self, year):
        """
        定位用户出生年份
        :param year:
        :return:
        """
        self.select_by_text(self.birthday_year_loc, year)

    def birthday_month(self, month):
        """
        定位用户出生月份
        :param month:
        :return:
        """
        self.select_by_text(self.birthday_month_loc, month)

    def birthday_day(self, day):
        """
        定位用户出生day
        :param day:
        :return:
        """
        self.select_by_text(self.birthday_day_loc, day)

    def sex(self, sex):
        if sex == "2":
            self.click(self.women_loc)
        elif sex == "1":
            self.click(self.man_loc)
        elif sex == "0":
            self.click(self.secrecy_loc)
        else:
            raise ("输入有误,只能输入'男 输入1','女 输入2','保密' 输入0")

    # 定位邮箱并输入
    def input_email(self, email):
        self.clear_send_text(self.email_loc, email)

    # 定位手qq号并输入
    def input_qq_number(self, qq_num):
        self.clear_send_text(self.qq_loc, qq_num)

    # 定位办公电话并输入
    def input_office_tel(self, office_tel):
        self.clear_send_text(self.office_tel_loc, office_tel)

    # 定位家庭电话并输入
    def input_home_tel(self, home_tel):
        self.clear_send_text(self.home_tel_loc, home_tel)

    # 定位手机号并输入
    def tel_num(self, phone):
        self.clear_send_text(self.extend_field5_loc, phone)

    # 定位密码提示问题
    def question(self, index):
        self.select_by_text(self.question_loc, index)

    # 定位密码问题答案
    def answer(self, answer):
        self.clear_send_text(self.answer_loc, answer)

    # 定位个人信息确认修改按钮
    def userinfo_enter(self):
        self.click(self.userinfo_enter_loc)

    def input_old_password(self, old_password):
        """
        定位原密码输入框
        :param old_password:
        :return:
        """
        self.send_text(self.old_password_loc, old_password)

    def input_new_password(self, new_password):
        """
        定位新密码输入框
        :param new_password:
        :return:
        """
        self.send_text(self.new_password_loc, new_password)

    def input_chick_password(self, chick_password):
        """
        定位确认密码输入
        :param chick_password:
        :return:
        """
        self.send_text(self.chick_password_loc, chick_password)

    def password_enter(self):
        """
        定位确认修改密码的确认按钮
        :return:
        """
        self.click(self.password_enter_loc)


#
if __name__ == '__main__':
    driver = open_op_browser(path)
    up = Userinfo_page(driver)
    up.open_url(login_url)
    up.click_userinfo()
    up.birthday_year("1995")
    up.birthday_month("11")
    up.birthday_day("29")
    up.sex("2")
    up.input_email("456@qq.com")
    up.input_qq_number("974937")
    up.input_office_tel("1278374")
    up.input_home_tel("2897498")
    up.tel_num("18947329347")
    up.question(3)
    up.answer("uhf u")
    # up.userinfo_enter()
    sleep(3)
    up.input_old_password("123456")
    sleep(3)
    up.input_new_password("123")
    up.input_chick_password("123")
    up.password_enter()
